<?php

    class Partner extends MY_Controller
    {
        public function __construct()
        {
            parent::__construct();
            $this->load->model('admin/partner_model');
        }
        public function index()
        {
            $data=$this->partner_model->getAll(10,0,-1);
            $this->load->vars('item',$data);
        }
        
        //add new partner
        public function add()
        {
        }
        
        //edit partner 
        public function edit()
        {
            $id=$this->uri->segment(4);
            $this->data['item']=$this->partner_model->getpartnerById($id);
            $this->load->vars('item',$this->data['item']);
        }
        
        //save new partner or update 
        public function save()
        {
           if ($this->form_validation->run() == FALSE)
            {
                $this->form_validation->set_error_delimiters('<div class="error" style="text-align:center;">', '</div>');
                $this->load->view('admin/form/form');
            }
            else
            {
                $post=$this->input->post();
                $id=$post['id'];
                $name=$post['name'];
                $phone=$post['phone'];
                $address=$post['address'];
                $date=$post['date'];
                $name_res=$post['name_res'];
                $date=$post['date'];
                $sex=$post['sex'];
                $phone_res=$post['phone_res'];
                $email_res=$post['email_res'];
                $description=$post['desciption'];
                $publish=$post['publish'];
                $order=$post['order'];
                $data=array();
                $data['partner_name']=$name;
                $data['partner_phone']=$phone;
                $data['partner_address']=$address;
                $data['partner_representative']=$name_res;
                $data['partner_representative_phone']=$phone_res;
                $data['partner_representative_sex']=$email_res;
                $data['partner_date']=$date;
                $data['partner_description']=$description;
                $data['partner_order']=$order;
                $data['partner_publish']=$publish;
                //call save
                if($this->partner_model->save($id,$data)>0)
                {
                    
                    //redirect(base_url()."admin/partner");
                }
                
            //}// End if formvalidation
            redirect(base_url()."admin/partner");
        }
    }
        //publish partner
        public function publish()
        {
            $id=$this->uri->segment(4);
            $this->partner_model->publish(1,array($id));
            redirect(base_url()."admin/partner");
        }
        //unpublish
        public function unpublish()
        {
            $id=$this->uri->segment(4);
            $this->partner_model->publish(0,array($id));
            redirect(base_url()."admin/partner");
        }
        //for actions from menu
        
        public function action()
        {
            $task=$this->input->post('task');
            $task=trim($task);
            $id=$this->input->post('id');
            switch($task)
            {
                case 'saveorder':
                $cid=$this->input->post('cid');
                $order=$this->input->post('order');
                $this->partner_model->saveorder($cid,$order);
                break;
                case 'delete':
                $this->partner_model->delete($id);
                break;
                case 'publish':
                $this->partner_model->publish(1,$id);
                break;
                case 'unpublish':
                $this->partner_model->publish(0,$id);
                break;
                
            }
            redirect(base_url()."admin/partner");
        }
    }